#!/bin/sh
#file: /usr/bin/dis
#disassemble a function 
#author: jusse@2013.12.12

function print_usage {
    echo "Usage:
    $0 <path-to-c-library> [function-name]
Eg:
    $0 /lib64/libc.so.6 fork
"
}

if [ $# -lt 1 ]; then
    print_usage
    exit 1;
fi


routine=$1 
func=$2 

if [ -z "$routine" ]; then 
    exit 
fi

start=$(nm -n $routine | grep "\w\s$func" | awk '{print "0x"$1;exit}')
end=$(nm -n $routine | sort -k1 -u |grep -A1 "\w\s$func" | awk '{getline; print "0x"$1;exit}') 

if [ -z "$func" ]; then
    objdump -d $routine
else 
    echo "start-address: $start, end-address: $end"
    objdump -d $routine --start-address=$start --stop-address=$end
fi
